package com.sscaipiao.android.activity;

import java.util.*;

import android.view.MenuItem;
import android.widget.*;
import com.sscaipiao.android.activity.Base.BaseHallListUIActivity;
import com.sscaipiao.android.activity.DaLeTou.DaLeTouActivity;
import com.sscaipiao.android.activity.QiLeCai.QiLeCaiActivity;
import com.sscaipiao.android.activity.ShiYiXuanWu.ShiYiXuanWuActivity;
import com.sscaipiao.android.activity.ShuangSeQiu.DoubleColorBallActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;

import com.sscaipiao.android.R;
import com.sscaipiao.android.activity.ThreeD.SanDListActivity;
import com.sscaipiao.android.common.Constants;
import com.sscaipiao.android.model.LotteryEntry;
import com.sscaipiao.android.model.TogetherBuy;
import com.sscaipiao.android.utils.CaipiaoServiceHelper;
import com.sscaipiao.android.adapter.TogetherBuyItemAdapt;

public class TogetherHallActivity extends BaseHallListUIActivity implements OnItemClickListener,  PopupMenu.OnMenuItemClickListener{

	private PopupWindow popupwindow;

	private ListView listView = null;
	private TogetherBuyItemAdapt adapt = null;
	private int lastItem = 0;
	private boolean reachLastPage = false;
	private String mCurrentLott;
	private TextView mTVCanYuJinDu;
	private TextView mTVFangAnZongE;
	private TextView mTVMeiFenJinE;
	private TextView mTVFanJiangLv;



	private int mOldOrderBy = 0;
	private int mOldOrder = 0;
	private List<TogetherBuy> mHeMaiList;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
				.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
				.penaltyLog().penaltyDeath().build());

		super.onCreate(savedInstanceState);
		addContainerMain(R.layout.together_hall);
//		setCallback(this);
		listView = (ListView) findViewById(R.id.hemai_hall_listView);
		mHeMaiList = new ArrayList<TogetherBuy>();

		listView.setOnItemClickListener(this);
		setupListOrderBarView();

	}

	@Override
	protected void onResume() {
		super.onResume();
		showProgressDialog();
		Thread thr1 = new Thread(mTask_GetAllLott, "1");
		thr1.start();

	}
	Runnable mTask_GetAllLott = new Runnable() {
		public void run() {
			List<String> lottlist = user.getLotteryList();
			if (lottlist == null || lottlist.size() <= 0) {
				user.setLotteryList(CaipiaoServiceHelper.getAllLottery(TogetherHallActivity.this, user));
				lottlist = user.getLotteryList();
			}
			mCurrentLott =  lottlist.get(0).split(Constants.STRING_SPLITE)[1];
			closeProgressDailog();
			final List<String> finalLottlist = lottlist;
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					setTitleText(finalLottlist.get(0).split(Constants.STRING_SPLITE)[2]);
					Thread thr2 = new Thread(mTask_GetHeMai, "2");
					thr2.start();
				}
			});
		}
	};
	Runnable mTask_GetHeMai = new Runnable() {
		public void run() {
			getHeMaiByLottCode(mCurrentLott,mOldOrderBy,mOldOrder);
			closeProgressDailog();
		}
	};

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (v.getId() == R.id.title_arrow) {
			//initmPopupWindowView();
			createPopupMenu();
		}
		if (v == mTVCanYuJinDu || v == mTVFangAnZongE || v == mTVFanJiangLv || v == mTVMeiFenJinE) {
			dealwithNewOrder((TextView) v);
		}
	}

	private void clearViewOrderStatus() {
		setViewOrderStatus(mTVCanYuJinDu, 0);
		setViewOrderStatus(mTVFangAnZongE, 0);
		setViewOrderStatus(mTVMeiFenJinE, 0);
		setViewOrderStatus(mTVFanJiangLv, 0);

	}

	private void getViewOrderStatus() {
		if ((Integer)(mTVCanYuJinDu.getTag()) != 0) {
			mOldOrderBy = TogetherBuyItemAdapt.ORDERBY_CanYuJinDu;
			mOldOrder = (Integer) mTVCanYuJinDu.getTag();
			return;
		}
		if ((Integer)(mTVFangAnZongE.getTag()) != 0) {
			mOldOrderBy = TogetherBuyItemAdapt.ORDERBY_FangAnZongE;
			mOldOrder = (Integer) mTVFangAnZongE.getTag();
			return;
		}
		if ((Integer)(mTVMeiFenJinE.getTag()) != 0) {
			mOldOrderBy = TogetherBuyItemAdapt.ORDERBY_MeiFenJinE;
			mOldOrder = (Integer) mTVMeiFenJinE.getTag();
			return;
		}
		if ((Integer)(mTVFanJiangLv.getTag()) != 0) {
			mOldOrderBy = TogetherBuyItemAdapt.ORDERBY_FanJiagLv;
			mOldOrder = (Integer) mTVFanJiangLv.getTag();
			return;
		}
	}

	private boolean checkListValueShoulReload(TextView v) {
		int oldOrderBy = mOldOrderBy;
		int newOrderBy = 0;
		if (v == mTVCanYuJinDu) {
			newOrderBy = TogetherBuyItemAdapt.ORDERBY_CanYuJinDu;
		}
		if (v == mTVFangAnZongE) {
			newOrderBy = TogetherBuyItemAdapt.ORDERBY_FangAnZongE;
		}
		if (v == mTVMeiFenJinE) {
			newOrderBy = TogetherBuyItemAdapt.ORDERBY_MeiFenJinE;
		}
		if (v == mTVFanJiangLv) {
			newOrderBy = TogetherBuyItemAdapt.ORDERBY_FanJiagLv;
		}
		if (oldOrderBy != newOrderBy) {
			return true;
		}
		return false;
	}

	private void dealwithNewOrder(TextView v) {
		int oldTag = (Integer)(v.getTag());
		getViewOrderStatus();
		clearViewOrderStatus();
		int newTag = 0;
		if (oldTag == TogetherBuyItemAdapt.ORDER_DOWN) {
			newTag = TogetherBuyItemAdapt.ORDER_UP;
		} else if (oldTag == TogetherBuyItemAdapt.ORDER_UP) {
			newTag = TogetherBuyItemAdapt.ORDER_DOWN;
		} else {
			newTag = TogetherBuyItemAdapt.ORDER_DOWN;
		}
		setViewOrderStatus(v,newTag);
		if (checkListValueShoulReload(v) == true) {
			showProgressDialog();
			updateListOrder();
		} else {
			adapt.setOrderKey(mOldOrderBy, newTag);
		}
	}


	private void updateListOrder() {
		getViewOrderStatus();
		Thread thr2 = new Thread(mTask_GetHeMai, "2");
		thr2.start();
	}

	private  void setupListOrderBarView() {
		mTVCanYuJinDu = (TextView) findViewById(R.id.tv_canyujindu);
		mTVFangAnZongE = (TextView) findViewById(R.id.tv_fanganzonge);
		mTVMeiFenJinE = (TextView) findViewById(R.id.tv_meifenjine);
		mTVFanJiangLv = (TextView) findViewById(R.id.tv_fanjianglv);

		mTVCanYuJinDu.setOnClickListener(this);
		mTVFangAnZongE.setOnClickListener(this);
		mTVMeiFenJinE.setOnClickListener(this);
		mTVFanJiangLv.setOnClickListener(this);


		setViewOrderStatus(mTVCanYuJinDu, TogetherBuyItemAdapt.ORDER_DOWN);
		setViewOrderStatus(mTVFangAnZongE, 0);
		setViewOrderStatus(mTVMeiFenJinE, 0);
		setViewOrderStatus(mTVFanJiangLv, 0);
		getViewOrderStatus();

	}

	private void getHeMaiByLottCode(String code, final int orderby, final int order) {
		mHeMaiList.clear();
		CaipiaoServiceHelper.getBetHmHall(this,mHeMaiList, user,code, null, null, null, null, null, null, null);
		if (mHeMaiList == null) {
			return;
		}
		if (mHeMaiList.size() == 0) {
			return;
		}
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if (adapt == null) {
					adapt = new TogetherBuyItemAdapt(TogetherHallActivity.this, mHeMaiList);
					listView.setAdapter(adapt);
				}
				mCurrentLott = mHeMaiList.get(0).getLotteryCode();
				adapt.setOrderKey(orderby,order);
			}
		});
	}


	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		String name = mHeMaiList.get(arg2).getLotteryName();
		Class activity = null;
		for (LotteryEntry v : Constants.LOTT_ARRAY) {
			if (v.getName().equals(name)) {
				activity = v.getActivity();
			}
		}
		if (activity == null) {
			Toast.makeText(this,"功能未实现",Toast.LENGTH_SHORT).show();
			return;
		}
		Intent intent = new Intent(this, activity);
		startActivity(intent);

	}

	private void createPopupMenu() {
		PopupMenu popup = new PopupMenu(this, findViewById(R.id.title_text));
		//popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());
		for (int j = 0; j < user.getLotteryList().size(); j++) {
			popup.getMenu().add(user.getLotteryList().get(j).split(Constants.STRING_SPLITE)[2]);
		}
		popup.setOnMenuItemClickListener(this);
		popup.show();

	}

	private String  getLottCodeByName(String name) {
		for (String value : user.getLotteryList()) {
			if (value.split(Constants.STRING_SPLITE)[2].equals(name)) {
				return value.split(Constants.STRING_SPLITE)[1];
			}
		}
		return null;

	}

	@Override
	public boolean onMenuItemClick(MenuItem item) {
		String code = getLottCodeByName(item.getTitle().toString());
		if (code == null) {
			return false;
		}
		if (code.equals(mCurrentLott)) {
			return false;
		}
		mCurrentLott =  code;
		showProgressDialog();
		Thread thr2 = new Thread(mTask_GetHeMai, "2");
		thr2.start();
		setTitleText(item.getTitle().toString());
		return false;
	}

	private void setViewOrderStatus(TextView tv, int state) {
		tv.setTag(state);
		setArrow(tv, state);
	}
	private void setArrow(TextView tv, int state) {
		switch (state) {
			case 0:
				tv.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
				break;
			case 1:
				tv.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(R.drawable.arrow_up), null);
				break;
			case 2:
				tv.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(R.drawable.arrow_down), null);
				break;
			default:
				tv.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);


		}
	}

}
